package replication;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.net.URL;
import java.net.URLConnection;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import javax.swing.JOptionPane;
import rmi.Server;

public class RegServer_Replication implements Serializable{
	Registry primaryRegistry = null;
	Registry secondaryRegistry = null;
	String primaryIp = null;
	String secondaryIp = null;
	String hostName = "http://www.andrew.cmu.edu/user/rshroff/ipaddress";
	String currentServer = "Backup";
	
	public RegServer_Replication() {
		
		primaryIp = null;
		secondaryIp = null;
		try {
			URL server = new URL(hostName);
			URLConnection yc = server.openConnection();
	        BufferedReader in = new BufferedReader(
	                                new InputStreamReader(
	                                yc.getInputStream()));
	        String inputLine;
	        
	        if ((inputLine = in.readLine()) != null) {
	        	primaryIp = inputLine;
	        }
	        if ((inputLine = in.readLine()) != null) {
	        	secondaryIp = inputLine;
	        }
	        
		} catch (Exception e){ /*this.shutDown();*/}
		System.out.println(primaryIp + secondaryIp);
		
		try {
			primaryRegistry = LocateRegistry.getRegistry(primaryIp);
			secondaryRegistry = LocateRegistry.getRegistry(secondaryIp);
		}
		catch(Exception e) {
			if (primaryRegistry == null) {
				this.shutDown();
			}
			System.out.println ("Unable to lookup registry. System exiting");
		}
	}

	public String getPrimaryIp() {
		return primaryIp;
	}

	public void setPrimaryIp(String primaryIp) {
		this.primaryIp = primaryIp;
	}

	public String getSecondaryIp() {
		return secondaryIp;
	}

	public void setSecondaryIp(String secondaryIp) {
		this.secondaryIp = secondaryIp;
	}

	public Server getServer () {
		
		Server svr = null;
		Registry reg = getWorkingRegistry();
		if (reg == null) {
			return null;
		}
		
		if (currentServer.equals("Backup")) {
			currentServer = "Primary";
			try {
				svr = (Server) reg.lookup("TecBox_PrimaryServer");
			} catch (Exception e) {}
		}
		else {
			currentServer = "Backup";
			try {
				svr = (Server) reg.lookup("TecBox_BackupServer");
			} catch (Exception e) {}
		}
		
		return svr;
	}

	private Registry getWorkingRegistry() {

		Registry reg = null;

		try {
			System.out.println (primaryRegistry.list()[0]);
			reg = primaryRegistry;
		} catch (Exception e) {
			try {
				System.out.println (secondaryRegistry.list()[0]);
				reg = secondaryRegistry;
			} catch (Exception e1) {
				shutDown();
			} 
		} 
		
		return reg;
	}

	public Registry getRegistryPrimaryIp() {
		return primaryRegistry;
	}

	public void setRegistryPrimaryIp(Registry registryPrimaryIp) {
		this.primaryRegistry = registryPrimaryIp;
	}

	public Registry getRegistrySecondaryIp() {
		return secondaryRegistry;
	}

	public void setRegistrySecondaryIp(Registry registrySecondaryIp) {
		this.secondaryRegistry = registrySecondaryIp;
	}

	public void shutDown() {
		
		JOptionPane.showMessageDialog(null,
				"TecBox System is experiencing some techinical problems. Please try again later",
				"Fatal error",
				JOptionPane.ERROR_MESSAGE);
		System.exit(-1);
		return;
	}
}
